package com.student.config;

import org.springframework.web.filter.CorsFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

@Configuration
public class CorsFilterConfig {
	@Bean
	public FilterRegistrationBean<CorsFilter> corsFilter(){
		FilterRegistrationBean<CorsFilter> corsFilterFilterRegistrationBean=new FilterRegistrationBean<>();
		UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
		CorsConfiguration configuration=new CorsConfiguration();
		configuration.addAllowedHeader("*");
		configuration.addAllowedOriginPattern("*");
		configuration.setAllowCredentials(true);
		configuration.setMaxAge(3600L);
		configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT","DELETE"));
		source.registerCorsConfiguration("/**",configuration);
		corsFilterFilterRegistrationBean.setFilter(new CorsFilter(source));
		corsFilterFilterRegistrationBean.setOrder(-1);
		return corsFilterFilterRegistrationBean;
	}
}
